/**
 * 
 */
package vue.popup;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * <p>
 * Abstract class use to choose specifique paramater of a solver/preprocesor.
 * </p>
 * <p>
 * iIt is provide with tool to select timeout and output file
 * </p>
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public abstract class IGDialogParameter extends JPanel implements ActionListener {

	private static final long	serialVersionUID		= 1L;
	public static final String	PARAMETER_TITLE			= "Parameters";
	public static final String	TIMEOUT_DESCRIPTION		= "Timeout (ms): ";
	public static final String	DEFAULT_OUT_DIRECTORY	= "/tmp";
	public static final String	OUT_TITLE				= "Output";
	public static final String	OUT_DESCRIPTION			= "Output file: ";
	public static final String	DIRECTORY_BUTTON		= "Search directory";
	public static final String	PREFIX_TMP_FILE			= "TMPCNF_";
	public static final String	SUFFIX_TMP_FILE			= ".cnf";

	// public static final String DEFAULT_OUT_NAME = "newcnf.cnf";

	protected JPanel			outPanel;
	protected JLabel			outDescritpion;
	protected JTextField		outField;
	protected JButton			outButton;
	protected static File		lastDirectory			= new File(DEFAULT_OUT_DIRECTORY);


	protected JTextField		timeoutField;


	public IGDialogParameter() {
		setBorder(BorderFactory.createTitledBorder(PARAMETER_TITLE));

		timeoutField = new JTextField();

		// out
		outPanel = new JPanel(new GridLayout(2, 1));

		JPanel tmp = new JPanel(new GridLayout(1, 3));
		outDescritpion = new JLabel(OUT_DESCRIPTION);
		tmp.add(outDescritpion);
		outButton = new JButton(DIRECTORY_BUTTON);
		outButton.addActionListener(this);
		tmp.add(new JLabel());
		tmp.add(outButton);

		outPanel.add(tmp);

		outField = new JTextField(lastDirectory.getAbsolutePath());
		outPanel.add(outField);
		outPanel.setBorder(BorderFactory.createTitledBorder(PARAMETER_TITLE));

		// panel.add(outPanel);
	}

	/**
	 * Check if the parameter are correct and update them in the parameter object
	 * 
	 * @return if the parameter are ok.
	 */
	public abstract boolean checkAndSetParameter();


	/**
	 * Refresh the information on the parameter.
	 */
	public abstract void refresh();


	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == outButton)
		{
			JFileChooser fc = new JFileChooser(lastDirectory);
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int returnVal = fc.showOpenDialog(null);

			if (returnVal == JFileChooser.APPROVE_OPTION)
			{
				// File file = fc.getSelectedFile();
				lastDirectory = fc.getSelectedFile();// new File(file.getParent());
				outField.setText(lastDirectory.getPath() + "/");
			}
		}
	}

}
